/**
 * Copyright 2014,Peak Tai 台俊峰(taijunfeng_it@sina.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.cenluan.activity.member;

import java.util.Date;

import com.cenluan.activity.Activity;
import com.cenluan.common.Const;
import com.cenluan.ext.jfinal.PkController;
import com.cenluan.kit.UUIDKit;
import com.cenluan.me.call.Call;
import com.cenluan.user.User;

public class ActivityMemberController extends PkController {
	/**
	 * 报名，申请加入活动
	 */
	public void apply() {
		User user = getSessionAttr(Const.SESSION_USER);
		Activity activity = Activity.dao.findById(getPara());
		ActivityMember member = ActivityMember.dao.findByUserIdAndActivityId(
				user.getStr("id"), activity.getStr("id"));
		if (member != null) {
			setAttr("msg", "您已经报过名了");
			renderJson();
			return;
		}
		if (activity.getDate("start").before(new Date())) {
			setAttr("msg", "活动已经开始，不能再报名");
			renderJson();
			return;
		}
		if (user.getStr("id").equals(activity.getStr("createrId"))) {
			setAttr("msg", "您不能申请加入自己发起的活动");
			renderJson();
			return;
		}
		member = new ActivityMember();
		member.set("id", UUIDKit.generateUUID());
		member.set("userId", user.getStr("id"));
		member.set("activityId", activity.getStr("id"));
		member.set("state", ActivityMember.STATE_APPLYING);
		member.set("applyTime", new Date());
		member.save();
		setAttr("ok", "true");
		renderJson();
	}

	/**
	 * 通过申请
	 */
	public void pass() {
		User user = getSessionAttr(Const.SESSION_USER);
		String userId = user.getStr("id");
		ActivityMember member = ActivityMember.dao.findById(getPara());
		Activity activity = member.getActivity();
		if (!userId.equals(activity.getStr("createrId"))) {
			setAttr("msg", "您无权限进行此操作");
			renderJson();
			return;
		}
		// 通知
		Call.save(userId, member.getStr("userId"),
				Call.TYPE_APPLY_ACTIVITY_PASSED, activity.getStr("id"));
		member.set("state", ActivityMember.STATE_ACTIVE);
		member.set("joinTime", new Date());
		member.keep("id", "state", "joinTime").update();
		setAttr("ok", true);
		renderJson();
	}

	/**
	 * 移除或者拒绝
	 */
	public void delete() {
		User user = getSessionAttr(Const.SESSION_USER);
		ActivityMember member = ActivityMember.dao.findById(getPara());
		Activity activity = member.getActivity();
		if (!user.getStr("id").equals(activity.getStr("createrId"))) {
			setAttr("msg", "您无权限进行此操作");
			renderJson();
			return;
		}
		member.delete();
		// 通知
		if (ActivityMember.STATE_APPLYING.equals(member.getStr("state"))) {
			Call.save(user.getStr("id"), member.getStr("userId"),
					Call.TYPE_APPLY_ACTIVITY_REFUSED, activity.getStr("id"));
		} else if (ActivityMember.STATE_ACTIVE.equals(member.getStr("state"))) {
			Call.save(user.getStr("id"), member.getStr("userId"),
					Call.TYPE_ACTIVITY_MEMBER_DELETED, activity.getStr("id"));
		}
		setAttr("ok", true);
		renderJson();
	}

	/**
	 * 查询活动成员列表
	 */
	public void index() {
		User user = getSessionAttr(Const.SESSION_USER);
		Activity activity = Activity.dao.findById(getPara());
		if (!Activity.STATE_ACTIVE.equals(activity.getStr("state"))) {
			renderError(403);
		}

		setAttr("activity", activity);

		boolean isCreater = user.getStr("id").equals(
				activity.getStr("createrId"));
		setAttr("isCreater", isCreater);

		setAttr("canJoin",
				activity.getDate("start").after(new Date())
						&& !isCreater
						&& ActivityMember.dao.findByUserIdAndActivityId(
								user.getStr("id"), getPara()) == null);

		setAttr("activeList", ActivityMemberService.list(getPara(),
				ActivityMember.STATE_ACTIVE));
		setAttr("applyList", ActivityMemberService.list(getPara(),
				ActivityMember.STATE_APPLYING));
	}

}
